from random import randint

from constants.Constants import *
from role.Missile import Missile
from role.Tank import Tank


class Enemy(Tank):
    max_step = 60

    def __init__(self, screen):
        super().__init__(screen, "enemy" + str(randint(1, 3)), top=200, left=randint(1, 11) * 100, speed=5)
        self.random_direction_count = 0
        self.step = Enemy.max_step
        self.random_direction()

    def random_direction(self):
        self.random_direction_count += 1
        r = randint(1, 5)
        if r == 1:
            self.direction = UP
            self.stop = False
        elif r == 2:
            self.direction = DOWN
            self.stop = False
        elif r == 3:
            self.direction = LEFT
            self.stop = False
        elif r == 4:
            self.direction = RIGHT
            self.stop = False
        elif r == 5:
            self.stop = True

    def random_missile(self):
        if self.random_direction_count > 1 and 5 == randint(1, 30):
            self.missiles.append(Missile(self))

    def play(self):
        if self.step == 0:
            self.random_direction()
            self.step = randint(Enemy.max_step // 2, Enemy.max_step)
        else:
            self.step -= 1

        self.random_missile()
        return super().play()
